"""
子类继承父类的过程中，可以对父类已有的方法给出新的实现版本（重新实现父类方法）
这个动作就称为重写（override）

自定义线程类：继承Thread类，重写run方法（指定线程要执行的任务）
"""
import random
import time

from threading import Thread


class DownloadThread(Thread):

    def __init__(self, filename):
        self.filename = filename
        super(DownloadThread, self).__init__()

    def run(self) -> None:
        start = time.time()
        print(f'开始下载{self.filename}.')
        time.sleep(random.randint(3, 8))
        print(f'{self.filename}下载完成.')
        end = time.time()
        print(f'下载{self.filename}花费时间: {end - start:.3f}秒')


def main():
    start = time.time()
    threads = [
        DownloadThread('Python从入门到住院.pdf'),
        DownloadThread('MySQL从删库到跑路.avi'),
        DownloadThread('Linux从精通到放弃.mp3')
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    end = time.time()
    print(f'总共花费时间: {end - start:.3f}秒')


if __name__ == '__main__':
    main()
